/*
 * WorxActionServlet
 * 
 * Created on Mar. 13, 2006
 * David K. Connell
 * Copyright (c) 2006 - Synergy Systems, Inc
 */
package org.puresilk.afx.base;

// java support
import javax.servlet.http.*;
import javax.servlet.*;

// third party support
import org.apache.log4j.Logger;
import org.puresilk.web.ActionServlet;

import org.puresilk.afx.data.*;
import org.puresilk.afx.util.*;

/**
 * Main controller servlet for EntryWorx Client.
 * Dispatches all Actions via its the
 * ActionsServlet superclass.
 *
 * @web:servlet name="ActionServlet" load-on-startup="" 
 * @author david connell
 * 
 */
public class SilkAfxServlet extends ActionServlet {
	// common component attributes
	private Logger logger = Logger.getLogger(SilkAfxServlet.class);
	private static final long serialVersionUID = 7870214489944624239L;
	private static String AFX_SVC_NAME = "SilkAfx"; 
	
	/**
	 * Initialize the ActionServlet machinery and 
	 * set up the namespace in the key manager.
	 */
	public void init(ServletConfig config) throws ServletException {
		// become a good PSOServlet
		super.init(config);
		setServletName("SilkAfxServlet");
        
		// Set up Service Name
		setAfxServiceName(getProperty("prefix"));

		// initialize and register with the Key Manager
		SilkAfxKeyManager wxMgr = SilkAfxKeyManager.getInstance();
		wxMgr.setKey(getAfxServiceName() + ".ActionServlet",this);

		// add properties to key manager
		wxMgr.setKey(getAfxServiceName() + ".AppProperties",this.props);
		
		// setup service database connectors
		setupConnectors();
		
		// set up application defaults
		setDefaultIndexPage(getProperty("indexPage"));
		setDefaultErrorPage(getProperty("errorPage"));
		
		// hello world
		logger.info(getServletName() + " is up and running");
	}
	
	/**
	 * Override target for application specific 
	 * SilkAfxConnector initialization.  Default 
	 * implementation will attempt to load the
	 * standard connector, AfxConnector.
	 */
	protected void setupConnectors() {
		
		// set up the default Afx connector
		String wxCon = getProperty("AfxConnector");
		if (wxCon != null) 
			SilkAfxKeyManager.getInstance().setKey(getAfxServiceName() + ".AfxConnector",new SilkAfxConnector(wxCon));

	}
	
	//-------------------------------------
	// servlet property access
	//
	/**
	 * retrieve servlet property from
	 * applications property file stored in
	 * the KeyManager.
	 */
	public String getAppProperty(String key){
		// retrieve the Key Manager
		SilkAfxKeyManager keyMgr = SilkAfxKeyManager.getInstance();
		
		return keyMgr.getProperty(getAfxServiceName() + "." + key);
	}

	//--------------------------------------
	// Application level overrides
	//
	
	/**
	 * Ensure that all non-login request happen
	 * within a valid session.  If there is no session
	 * and it is not a login request, set an error message
	 * and goto the login page.
	 * 
	 * 	@param req the http request object 
	 * 	@return pass or fail 
	 */
	protected boolean sessionCheck(HttpServletRequest req) {
		String action = req.getParameter("action");
		HttpSession sess = req.getSession();			
		boolean rv = true;
		
		// if no session available and not a login 
		// request, force a trip to the login page
		if ( (sess == null) && (! "access.Login".equals(action) ) ) {
			rv = false;
			req.setAttribute("errmsg", "Your Session has Timed Out.");
		}
		return rv;
	}

	/*----------------------------------------------
	 * Setup standard good page / bad page values.
	 *----------------------------------------------*/
	protected void setDefaultIndexPage(String defIndexPage) {
		setDefaultNoTargetPage(defIndexPage);
	}
	
	public String getDefaultIndexPage() {
		return getDefaultNoTargetPage();
	}

	/*---------------------------------------------------
	 * Servlet name for logging etc...
	 *---------------------------------------------------*/
	public void setServletName(String nm) {
		servletName = nm;
	}
	
	public String getServletName() {
		return servletName;
	}
	
	/*------------------------------------------------------
	 * Setting of the service name for key manager domains.
	 *------------------------------------------------------*/
	protected void setAfxServiceName(String nm){
		AFX_SVC_NAME = nm;
	}
	
	protected String getAfxServiceName() {
		return AFX_SVC_NAME;
	}
}
